package com.weme.common.view

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.util.TypedValue
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat
import com.alibaba.android.arouter.utils.TextUtils
import com.weme.common.R
import com.weme.common.base.BaseDialog
import com.weme.common.databinding.DialogBottomSelectionBinding
import com.weme.common.util.dpToPx
import com.weme.common.util.visible


data class SelectionItem(val title: String, val onClickPosition: Int, val listener: BottomSelectionListener)
interface BottomSelectionListener {
    fun onTitleClicked(position: Int)
}

class BottomSelectionDialog : BaseDialog() {
    //  private var titles: Array<out String>? = null
    private var selectionItems: List<SelectionItem>? = null
    private lateinit var binding: DialogBottomSelectionBinding

    companion object {
        fun newInstance(items: List<SelectionItem>): BottomSelectionDialog {
            val fragment = BottomSelectionDialog()
            fragment.selectionItems = items
            return fragment
        }
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DialogBottomSelectionBinding.inflate(inflater, container, false)

        return binding.root
    }

    override fun onStart() {
        super.onStart()
        dialog?.window?.apply {
            attributes.windowAnimations = R.style.DialogAnimation
            this.setWindowAnimations(R.style.SlideUpAnimation)
            setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
            setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            val params = attributes
            params.gravity = Gravity.BOTTOM // 将对话框的重心设置为底部
            params.width = ViewGroup.LayoutParams.MATCH_PARENT // 设置对话框宽度为全屏
            attributes = params
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        if(!TextUtils.isEmpty(Title)){
            binding.tvTitle.text = Title
            binding.tvTitle.visible()
            binding.llView.visible()
            binding.SrcTtile.visible()
        }

        binding.llSelect.post {
            selectionItems?.forEachIndexed { index, item ->
                if(index==0){
                    binding.llSelect.addView(createDivider(requireActivity()))
                }
                val textView = TextView(context)
                val typeface = ResourcesCompat.getFont(requireActivity(), com.example.resources.R.font.book)
                textView.text = item.title
                textView.setTextColor(ContextCompat.getColor(requireActivity(), R.color.black))
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14f)
                textView.gravity = Gravity.CENTER
                textView.typeface = typeface
                textView.setBackgroundResource(R.color.white)
                textView.setPadding(0, 20.dpToPx(requireActivity()), 0, 20.dpToPx(requireActivity()))  // 自定义 dpToPx 扩展函数
                textView.setOnClickListener {
                    item.listener.onTitleClicked(item.onClickPosition)
                    dismiss()
                }
                binding.llSelect.addView(textView)
                if (index < selectionItems!!.size - 1) {
                    binding.llSelect.addView(createDivider(requireActivity()))
                }

            }
            binding.llSelect.requestLayout()
        }
        binding.cancel.setOnClickListener {
            dismiss()
        }
    }




    private fun createDivider(context: Context): View {
        val divider = View(context)
        val params = LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            1 // Divider height in pixels
        )
        params.setMargins(12.dpToPx(context), 0, 12.dpToPx(context), 0) // Optional margins for spacing
        divider.layoutParams = params
        divider.setBackgroundColor(Color.parseColor("#1A000000")) // Set the divider color
        return divider
    }
    private var Title = ""
    fun setTitle(title: String){
        Title = title
    }


}